package com.fg.cloud.factory.controller.factory;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.factory.LogisticsEntity;
import com.fg.cloud.manager.service.factory.LogisticsService;

/**
 * Description:物流公司业务控制
 * 
 * @author shaoqiping
 * @date 2017年8月7日下午2:08:45
 */
@Controller
@RequestMapping("/logistics")
public class LogisticsController extends BaseController {

	@Autowired
	private LogisticsService logisticsService;

	/**
	 * Description:物流公司添加
	 * 
	 * @param request
	 * @param response
	 * @param logisticsEntity
	 * @author shaoqiping
	 * @date 2017年8月7日上午10:17:20
	 */
	@RequestMapping(value = "/insert", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void insert(HttpServletRequest request, HttpServletResponse response, LogisticsEntity logisticsEntity) {
		try {
			if (logisticsEntity == null || StringUtils.isEmpty(logisticsEntity.getLogisticsName())) {
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			logisticsEntity.setFacId(getCurrentFactoryUser().getFacId());
			if (logisticsService.findByLogisticsName(logisticsEntity) != null) {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.LOGISTICS_REPEAT);
				return;
			}
			setCreateInfo(logisticsEntity, request);
			setUpdateInfo(logisticsEntity, request);
			boolean flag = logisticsService.insertSelective(logisticsEntity);
			if (flag) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, ResultType.ADD_SUCCESS);
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.ADD_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:查询所有物流公司
	 * 
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param filterText
	 * @author shaoqiping
	 * @date 2017年8月7日下午5:44:07
	 */
	@RequestMapping(value = "/findAll", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void findAll(HttpServletRequest request, HttpServletResponse response) {
		try {
			doResult(response, true, StatusCode.FINDSUCCESS,
					logisticsService.findAll(getCurrentFactoryUser().getFacId()), ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:条件列表查询
	 * 
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param filterText
	 * @author shaoqiping
	 * @date 2017年8月7日下午5:44:07
	 */
	@RequestMapping(value = "/findList", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void selectPage(HttpServletRequest request, HttpServletResponse response, Integer page, Integer rows,
			String sort, String order, String filterText) {
		try {
			if (page == null || page <= 0) {
				page = 1;
			}
			if (rows == null || rows <= 0) {
				rows = 10;
			}
			PageUtils<LogisticsEntity> pageinfo = new PageUtils<LogisticsEntity>(page, rows, sort, order);
			Map<String, Object> condition = pageinfo.getCondition();
			condition.put("filterText", filterText);
			condition.put("facId", getCurrentFactoryUser().getFacId());
			pageinfo.setCondition(condition);
			logisticsService.findList(pageinfo);
			doResult(response, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:根据ID单个查询物流公司信息
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @author shaoqiping
	 * @date 2017年8月7日上午11:45:56
	 */
	@RequestMapping(value = "/find", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void find(HttpServletRequest request, HttpServletResponse response, Long id) {
		try {
			if (id == null || LongUtils.isEmpty(id)) {
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.CHOOSE_INFO);
				return;
			}
			LogisticsEntity loginstics = logisticsService.find(id);
			if (loginstics != null && loginstics.getFacId().equals(getCurrentFactoryUser().getFacId())) {
				doResult(response, true, StatusCode.FINDSUCCESS, loginstics, ResultType.FIND_SUCCESS);
			} else {
				doResult(response, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, true, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:更新物流公司信息
	 * 
	 * @param request
	 * @param response
	 * @param loginsticsEntity
	 * @author shaoqiping
	 * @date 2017年8月7日上午11:53:38
	 */
	@RequestMapping(value = "/modify", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void update(HttpServletRequest request, HttpServletResponse response, LogisticsEntity logisticsEntity) {
		try {
			if (null == logisticsEntity || null == logisticsEntity.getId()) {
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			logisticsEntity.setFacId(getCurrentFactoryUser().getFacId());
			LogisticsEntity temp = logisticsService.findByLogisticsName(logisticsEntity);
			if (temp != null && !temp.getId().equals(logisticsEntity.getId())) {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.LOGISTICS_REPEAT);
				return;
			}
			setUpdateInfo(logisticsEntity, request);
			int flag = logisticsService.update(logisticsEntity);
			if (flag > 0) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:删除指定物流公司信息 (逻辑删除)
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @author shaoqiping
	 * @date 2017年8月7日下午2:07:41
	 */
	@RequestMapping(value = "/deletes", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public void delete(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "ids") String ids) {
		try {
			if (ids == null || StringUtils.isEmpty(ids)) {
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.CHOOSE_INFO);
				return;
			}
			Map<String, Object> map = new HashMap<>();
			map.put("ids", ids);
			map.put("facId", getCurrentFactoryUser().getFacId());
			boolean flag = logisticsService.deletes(map);
			if (flag) {
				doResult(response, true, StatusCode.REMOVESUCCESS, null, ResultType.REMOVE_SUCCESS);
			} else {
				doResult(response, false, StatusCode.REMOVEERROR, null, ResultType.REMOVE_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
}
